<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Device485 extends Model
{
    use HasFactory, SoftDeletes;

    protected $table = 'device_485';

    protected $fillable = ['device_id', 'model_id', 'address'];

    protected $appends = ['DeviceName', 'SensorName'];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function device()
    {
        return $this->belongsTo(Device::class, 'device_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function deviceModel()
    {
        return $this->belongsTo(Device485Model::class, 'model_id');
    }

    /**
     * @return mixed
     */
    public function getDeviceNameAttribute()
    {
        return $this->device ? $this->device->name : '未知设备';
    }

    /**
     * @return mixed
     */
    public function getSensorNameAttribute()
    {
        return $this->deviceModel->name ? $this->deviceModel->name : '未知传感器';
    }

}
